﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.XtraLayout;
using Pharmacy;
using VCS.Data;
using VCS.Entities;
using VCS.Settings.BusinessLayer;
using VCS.Windows.Forms.Properties;
using WinUtilities;
using WinUtilities.Class;

namespace VCS.Windows.Forms.Form.Main
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FormCompany : DevExpress.XtraEditors.XtraForm
    {
        /// <summary>
        /// 
        /// </summary>

        private List<Control> _requireValue;
        /// <summary>
        /// 
        /// </summary>
        public bool mIsInsert = true;
        private Company ObjCompany;
        /// <summary>
        /// 
        /// </summary>
        public FormCompany()
        {
            InitializeComponent();
            HighLightControl();
            //Fill Data
            var allPaymentMode = DataRepository.PaymentModeProvider.GetAll();
            txtPaymentMode.Properties.DataSource = allPaymentMode;
            txtPaymentMode.Properties.DisplayMember = "PaymentMode";
            txtPaymentMode.Properties.ValueMember = "PaymentMode";
            txtPaymentMode.KeyPress += GlobalUtilities.SearchLookUpEditFirstKeyPress;

            //Event For itemControl
            itemControlsSearch.DisplaySearchResult += DisplaySearchResult;
            itemControlsSearch.DisplayLogResult += DisplayLogResult;
            itemControlsSearch.SearchMethod = SearchMethods.Contains;
            itemControlsSearch.ButtonCancelClick += itemControlsSearch_ButtonCancelClick;
            itemControlsSearch.ButtonCloseClick += itemControlsSearch_ButtonCloseClick;
            itemControlsSearch.ButtonDeleteClick += itemControlsSearch_ButtonDeleteClick;
            itemControlsSearch.ButtonEditClick += itemControlsSearch_ButtonEditClick;
            itemControlsSearch.ButtonFindClick += itemControlsSearch_ButtonFindClick;
            itemControlsSearch.ButtonNewClick += itemControlsSearch_ButtonNewClick;
            itemControlsSearch.ButtonSaveClick += itemControlsSearch_ButtonSaveClick;

            // Passing hotkey from Form to itemControlSearch
            //KeyDown += GlobalKeyDown;
            //Lock Layout
            DisableLayout(false);
        }

        private DataSet DisplayLogResult()
        {
            return GlobalUtilities.ReadLog(ObjCompany, DataRepository.Provider.ExecuteDataSet);
        }

        private void DisableLayout(bool IsEnabled)
        {
            GlobalUtilities.ControlEnable(IsEnabled, Layout_Info);
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (GlobalUtilities.GlobalProcessCmdKey(this, itemControlsSearch.TrappedKeyDown, ref msg, keyData))
                return true;
            return base.ProcessCmdKey(ref msg, keyData);
        }

        /// <summary>
        /// 
        /// </summary>
        public void ClearTextBox()
        {
            GlobalUtilities.ClearTextbox(Layout_Info);
        }

        private void HighLightControl()
        {
            IntializeRequireValue();
            foreach (LayoutControlItem item in Layout_Info.Root.Items)
            {
                if (_requireValue.Contains(item.Control))
                    item.AppearanceItemCaption.ForeColor = CommonColors.GetCriticalColor(UserLookAndFeel.Default.ActiveLookAndFeel);
            }
        }

        private void IntializeRequireValue()
        {
            _requireValue = new List<Control> { txtCompanyName, txtAddress };
        }

        void GetInfo(ref Company objCompany)
        {
            if (mIsInsert)
            {
                objCompany = new Company { CreateDate = DateTime.Now, CreateUser = Program.GlobalUser.UserId };
                string companyCode = "";
                DataRepository.CompanyProvider.GetCompanyCode(ServiceFacade.SettingsHelper.LocationCode, ref companyCode);
                objCompany.CompanyCode = companyCode;
            }
            objCompany.AccountCode = txtAccountCode.Text;
            objCompany.Address = txtAddress.Text;
            objCompany.Attn = txtAttnName.Text;
            objCompany.AttnEmail = txtAttnEmail.Text;
            objCompany.AttnPhone = txtAttnPhone.Text;
            objCompany.BillingAddress = txtBillingAddress.Text;
            objCompany.TaxNumber = txtTaxCode.Text;
            objCompany.CompanyName = txtCompanyName.Text;
            objCompany.CompanyTel = txtTelephone1.Text;
            objCompany.CompanyTel2 = txtTelephone2.Text;
            objCompany.TypeOfServices = txtTypeOfService.Text;
            if (txtPaymentMode.EditValue != null) objCompany.PaymentMode = txtPaymentMode.EditValue.ToString();
            objCompany.Remark = txtRemark.Text;
        }

        /// <summary>
        /// Get Data From Form
        /// </summary>
        /// <param name="objCompany"></param>
        public void LoadUpdate(Company objCompany)
        {
            txtCompanyCode.Text = objCompany.CompanyCode;
            txtAccountCode.Text = objCompany.AccountCode;
            txtAddress.Text = objCompany.Address;
            txtAttnName.Text = objCompany.Attn;
            txtAttnEmail.Text = objCompany.AttnEmail;
            txtAttnPhone.Text = objCompany.AttnPhone;
            txtTaxCode.Text = objCompany.TaxNumber;
            txtBillingAddress.Text = objCompany.BillingAddress;
            txtCompanyName.Text = objCompany.CompanyName;
            txtTelephone1.Text = objCompany.CompanyTel;
            txtTelephone2.Text = objCompany.CompanyTel2;
            txtRemark.Text = objCompany.Remark;
            txtTypeOfService.Text = objCompany.TypeOfServices;
            txtPaymentMode.EditValue = objCompany.PaymentMode;
        }

        void DisplaySearchResult(object resultRow)
        {
            ObjCompany = (Company)resultRow;
            LoadUpdate(ObjCompany);
        }

        private void SetDefaultValue()
        {
            //Set default value when new here
        }

        private IBindingListView itemControlsSearch_ButtonFindClick()
        {
            int count;
            // Search Methods
            TList<Company> searchResult = DataRepository.CompanyProvider.GetPaged(itemControlsSearch.WhereExpression, "", 0, 100, out count);
            return searchResult;
        }

        private CommandStatus itemControlsSearch_ButtonSaveClick(object sender, EventArgs e)
        {
            bool check = GlobalUtilities.CheckInfo(_requireValue, Layout_Info);
            if (check)
            {
                if (mIsInsert)
                {
                    if (MessageBox.Show(Resources.InsertNotice, Resources.InsertMenu,
                               MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        GetInfo(ref ObjCompany);
                        ObjCompany.UpdateUser = Program.GlobalUser.UserId;
                        ObjCompany.UpdateDate = DateTime.Now;
                        DataRepository.CompanyProvider.Insert(ObjCompany);
                        txtCompanyCode.Text = ObjCompany.CompanyCode;
                        DisableLayout(false);
                        return CommandStatus.Inserted;
                    }
                    return CommandStatus.Canceled;
                }
                if (MessageBox.Show(Resources.UpdateNotice, Resources.UpdateMenu,
                           MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    var oldCompany = ObjCompany.Clone();
                    GetInfo(ref ObjCompany);
                    GlobalUtilities.WriteLog(oldCompany, ObjCompany, Program.GlobalUser.UserId, DataRepository.Provider.ExecuteDataSet);
                    ObjCompany.UpdateUser = Program.GlobalUser.UserId;
                    ObjCompany.UpdateDate = DateTime.Now;
                    DataRepository.CompanyProvider.Update(ObjCompany);
                    DisableLayout(false);
                    return CommandStatus.Updated;
                }
                return CommandStatus.Canceled;
            }
            return CommandStatus.Canceled;
        }

        private void itemControlsSearch_ButtonNewClick(object sender, EventArgs e)
        {
            ClearTextBox();
            mIsInsert = true;
            DisableLayout(true);
            SetDefaultValue();
            txtCompanyName.Focus();
        }

        private void itemControlsSearch_ButtonEditClick(object sender, EventArgs e)
        {
            mIsInsert = false;
            DisableLayout(true);
            txtCompanyName.Focus();
        }

        private CommandStatus itemControlsSearch_ButtonDeleteClick(object sender, EventArgs e)
        {
            if (MessageBox.Show(Resources.DeleteNotice, Resources.DeleteMenu,
                                       MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                DataRepository.CompanyProvider.Delete(ObjCompany);
                ClearTextBox();
                return CommandStatus.Deleted;
            }
            return CommandStatus.Canceled;
        }

        private void itemControlsSearch_ButtonCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private CommandStatus itemControlsSearch_ButtonCancelClick(object sender, EventArgs e)
        {
            bool warning = MessageBox.Show(Resources.CancelNotice, Resources.CancelMenu, MessageBoxButtons.YesNoCancel) == DialogResult.Yes;
            if (warning)
            {
                if (mIsInsert)
                {
                    ClearTextBox();
                    DisableLayout(false);
                    return CommandStatus.Inserted;
                }
                LoadUpdate(ObjCompany);
                DisableLayout(false);
                return CommandStatus.Updated;
            }
            return CommandStatus.Canceled;
        }
    }
}